package com.vcc.web.core.aspect;

import com.vcc.core.exception.PermissionException;
import com.vcc.core.exception.SignInException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;

@Component
@Aspect
public class VccROAspect {

    private static final Logger logger = LoggerFactory.getLogger(VccROAspect.class);

    //controller层切点
    @Pointcut("@annotation(com.vcc.core.annotations.ROReject)")
    public void controllerAspect() {
    }

    ;

    @Before("controllerAspect()")
    public void deBefore(JoinPoint joinPoint) throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (!StringUtils.isEmpty( request.getAttribute("ro"))) {
            if ((Boolean) request.getAttribute("ro")) {
                throw new PermissionException("RO，无法访问");
            }
        }
    }


}
